import { MigrationInterface, QueryRunner } from 'typeorm'

export class Init1739357541594 implements MigrationInterface {
  name = 'Init1739357541594'

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`CREATE TABLE \`permissions\` (\`id\` int NOT NULL AUTO_INCREMENT, \`code\` varchar(20) NOT NULL COMMENT '权限代码', \`description\` varchar(100) NOT NULL COMMENT '权限描述', \`createTime\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), \`updateTime\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), UNIQUE INDEX \`IDX_8dad765629e83229da6feda1c1\` (\`code\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`)
    await queryRunner.query(`CREATE TABLE \`menu\` (\`id\` int NOT NULL AUTO_INCREMENT, \`name\` varchar(20) NOT NULL COMMENT '菜单名称', \`path\` varchar(255) NOT NULL COMMENT '菜单的url', \`redirect\` varchar(255) NOT NULL COMMENT '重定向地址', \`metaTitle\` varchar(255) NOT NULL COMMENT '菜单的meta title信息', \`metaIcon\` varchar(255) NOT NULL COMMENT '菜单的meta icon信息', \`metaActiveMenu\` varchar(255) NOT NULL COMMENT '菜单的meta activeMenu信息', \`pid\` int NOT NULL COMMENT '父菜单的id', \`active\` int NOT NULL COMMENT '菜单状态 1: 可用 0: 不可用' DEFAULT '1', \`createTime\` datetime(6) NOT NULL COMMENT '创建时间' DEFAULT CURRENT_TIMESTAMP(6), \`updateTime\` datetime(6) NOT NULL COMMENT '更新时间' DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), UNIQUE INDEX \`IDX_59140852a09bf896aacf214028\` (\`path\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`)
    await queryRunner.query(`CREATE TABLE \`roles\` (\`id\` int NOT NULL AUTO_INCREMENT, \`name\` varchar(20) NOT NULL COMMENT '角色名', \`description\` varchar(100) NOT NULL COMMENT '角色描述', \`createTime\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), \`updateTime\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), UNIQUE INDEX \`IDX_648e3f5447f725579d7d4ffdfb\` (\`name\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`)
    await queryRunner.query(`CREATE TABLE \`users\` (\`id\` int NOT NULL AUTO_INCREMENT, \`username\` varchar(50) NOT NULL COMMENT '用户名', \`password\` varchar(100) NOT NULL COMMENT '密码', \`nick_name\` varchar(50) NOT NULL COMMENT '昵称', \`email\` varchar(50) NOT NULL COMMENT '邮箱', \`headPic\` varchar(100) NULL COMMENT '头像', \`phoneNumber\` varchar(20) NULL COMMENT '手机号', \`isFrozen\` tinyint NOT NULL COMMENT '是否冻结' DEFAULT 0, \`isAdmin\` tinyint NOT NULL COMMENT '是否是管理员' DEFAULT 0, \`createTime\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), \`updateTime\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), \`loginType\` int NOT NULL COMMENT '登录类型, 0 用户名密码登录, 1 Google 登录, 2 Github 登录' DEFAULT '0', UNIQUE INDEX \`IDX_fe0bb3f6520ee0469504521e71\` (\`username\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`)
    await queryRunner.query(`CREATE TABLE \`history\` (\`id\` int NOT NULL AUTO_INCREMENT, \`username\` varchar(50) NOT NULL COMMENT '用户名', \`ip\` varchar(50) NOT NULL COMMENT '用户IP', \`host\` varchar(50) NOT NULL COMMENT '用户主机', \`createTime\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`)
    await queryRunner.query(`CREATE TABLE \`meeting\` (\`id\` int NOT NULL AUTO_INCREMENT COMMENT '会议室ID', \`name\` varchar(50) NOT NULL COMMENT '会议室名字', \`capacity\` int NOT NULL COMMENT '会议室容量', \`location\` varchar(50) NOT NULL COMMENT '会议室位置', \`equipment\` varchar(50) NOT NULL COMMENT '设备' DEFAULT '', \`description\` varchar(100) NOT NULL COMMENT '描述' DEFAULT '', \`isBooked\` tinyint NOT NULL COMMENT '是否被预订' DEFAULT 0, \`createTime\` datetime(6) NOT NULL COMMENT '创建时间' DEFAULT CURRENT_TIMESTAMP(6), \`updateTime\` datetime(6) NOT NULL COMMENT '更新时间' DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`)
    await queryRunner.query(`CREATE TABLE \`role_menu\` (\`menuId\` int NOT NULL, \`rolesId\` int NOT NULL, INDEX \`IDX_ed7dbf72cc845b0c9150a67851\` (\`menuId\`), INDEX \`IDX_e514ec73ca15187d43e56511a6\` (\`rolesId\`), PRIMARY KEY (\`menuId\`, \`rolesId\`)) ENGINE=InnoDB`)
    await queryRunner.query(`CREATE TABLE \`role_permissions\` (\`rolesId\` int NOT NULL, \`permissionsId\` int NOT NULL, INDEX \`IDX_0cb93c5877d37e954e2aa59e52\` (\`rolesId\`), INDEX \`IDX_d422dabc78ff74a8dab6583da0\` (\`permissionsId\`), PRIMARY KEY (\`rolesId\`, \`permissionsId\`)) ENGINE=InnoDB`)
    await queryRunner.query(`CREATE TABLE \`user_roles\` (\`usersId\` int NOT NULL, \`rolesId\` int NOT NULL, INDEX \`IDX_99b019339f52c63ae615358738\` (\`usersId\`), INDEX \`IDX_13380e7efec83468d73fc37938\` (\`rolesId\`), PRIMARY KEY (\`usersId\`, \`rolesId\`)) ENGINE=InnoDB`)
    await queryRunner.query(`ALTER TABLE \`role_menu\` ADD CONSTRAINT \`FK_ed7dbf72cc845b0c9150a678512\` FOREIGN KEY (\`menuId\`) REFERENCES \`menu\`(\`id\`) ON DELETE CASCADE ON UPDATE CASCADE`)
    await queryRunner.query(`ALTER TABLE \`role_menu\` ADD CONSTRAINT \`FK_e514ec73ca15187d43e56511a6a\` FOREIGN KEY (\`rolesId\`) REFERENCES \`roles\`(\`id\`) ON DELETE CASCADE ON UPDATE CASCADE`)
    await queryRunner.query(`ALTER TABLE \`role_permissions\` ADD CONSTRAINT \`FK_0cb93c5877d37e954e2aa59e52c\` FOREIGN KEY (\`rolesId\`) REFERENCES \`roles\`(\`id\`) ON DELETE CASCADE ON UPDATE CASCADE`)
    await queryRunner.query(`ALTER TABLE \`role_permissions\` ADD CONSTRAINT \`FK_d422dabc78ff74a8dab6583da02\` FOREIGN KEY (\`permissionsId\`) REFERENCES \`permissions\`(\`id\`) ON DELETE CASCADE ON UPDATE CASCADE`)
    await queryRunner.query(`ALTER TABLE \`user_roles\` ADD CONSTRAINT \`FK_99b019339f52c63ae6153587380\` FOREIGN KEY (\`usersId\`) REFERENCES \`users\`(\`id\`) ON DELETE CASCADE ON UPDATE CASCADE`)
    await queryRunner.query(`ALTER TABLE \`user_roles\` ADD CONSTRAINT \`FK_13380e7efec83468d73fc37938e\` FOREIGN KEY (\`rolesId\`) REFERENCES \`roles\`(\`id\`) ON DELETE CASCADE ON UPDATE CASCADE`)
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`ALTER TABLE \`user_roles\` DROP FOREIGN KEY \`FK_13380e7efec83468d73fc37938e\``)
    await queryRunner.query(`ALTER TABLE \`user_roles\` DROP FOREIGN KEY \`FK_99b019339f52c63ae6153587380\``)
    await queryRunner.query(`ALTER TABLE \`role_permissions\` DROP FOREIGN KEY \`FK_d422dabc78ff74a8dab6583da02\``)
    await queryRunner.query(`ALTER TABLE \`role_permissions\` DROP FOREIGN KEY \`FK_0cb93c5877d37e954e2aa59e52c\``)
    await queryRunner.query(`ALTER TABLE \`role_menu\` DROP FOREIGN KEY \`FK_e514ec73ca15187d43e56511a6a\``)
    await queryRunner.query(`ALTER TABLE \`role_menu\` DROP FOREIGN KEY \`FK_ed7dbf72cc845b0c9150a678512\``)
    await queryRunner.query(`DROP INDEX \`IDX_13380e7efec83468d73fc37938\` ON \`user_roles\``)
    await queryRunner.query(`DROP INDEX \`IDX_99b019339f52c63ae615358738\` ON \`user_roles\``)
    await queryRunner.query(`DROP TABLE \`user_roles\``)
    await queryRunner.query(`DROP INDEX \`IDX_d422dabc78ff74a8dab6583da0\` ON \`role_permissions\``)
    await queryRunner.query(`DROP INDEX \`IDX_0cb93c5877d37e954e2aa59e52\` ON \`role_permissions\``)
    await queryRunner.query(`DROP TABLE \`role_permissions\``)
    await queryRunner.query(`DROP INDEX \`IDX_e514ec73ca15187d43e56511a6\` ON \`role_menu\``)
    await queryRunner.query(`DROP INDEX \`IDX_ed7dbf72cc845b0c9150a67851\` ON \`role_menu\``)
    await queryRunner.query(`DROP TABLE \`role_menu\``)
    await queryRunner.query(`DROP TABLE \`meeting\``)
    await queryRunner.query(`DROP TABLE \`history\``)
    await queryRunner.query(`DROP INDEX \`IDX_fe0bb3f6520ee0469504521e71\` ON \`users\``)
    await queryRunner.query(`DROP TABLE \`users\``)
    await queryRunner.query(`DROP INDEX \`IDX_648e3f5447f725579d7d4ffdfb\` ON \`roles\``)
    await queryRunner.query(`DROP TABLE \`roles\``)
    await queryRunner.query(`DROP INDEX \`IDX_59140852a09bf896aacf214028\` ON \`menu\``)
    await queryRunner.query(`DROP TABLE \`menu\``)
    await queryRunner.query(`DROP INDEX \`IDX_8dad765629e83229da6feda1c1\` ON \`permissions\``)
    await queryRunner.query(`DROP TABLE \`permissions\``)
  }
}
